package com.example.book.controller;


import com.example.book.constant.Constants;
import com.example.book.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public Boolean login(String name, String password, HttpSession session){
        log.info("用户登录, name:{}", name);
        if(!StringUtils.hasLength(name) || !StringUtils.hasLength(password)){
            return false;
        }
        //校验账号密码是否正确
        Boolean result = userService.checkPassword(name, password);
        if (result){
            session.setAttribute(Constants.SESSION_LOGIN_USER_NAME, name);
        }
        return result;
    }
}
